package com.dongdongshop.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongdongshop.pojo.TbItemCat;
import com.dongdongshop.pojo.TbTypeTemplate;
import com.dongdongshop.result.Result;
import com.dongdongshop.result.ResultEnum;
import com.dongdongshop.service.ItemCatService;
import com.dongdongshop.service.TypeTemplateService;
import com.dongdongshop.util.PageUtil;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@RequestMapping("itemCat")
@Controller
public class ItemCatController {

    private static final Logger logger = LoggerFactory.getLogger(ItemCatController.class);

    @Reference
    private ItemCatService itemCatService;

    @Reference
    private TypeTemplateService typeTemplateService;

    @RequestMapping("toItemCatShow")
    public String toItemCatShow(Model model){
        List<TbTypeTemplate> templates = typeTemplateService.getTemplateAll();
        model.addAttribute("templates",templates);
        return "admin/item_cat";
    }

    @RequestMapping("getListItemCat")
    @ResponseBody
    public Result<PageUtil<TbItemCat>> getListItemCat(@RequestParam(value = "pageNumber", defaultValue = "1") Integer pageNumber,
                                                      @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                                      @RequestParam(defaultValue = "0") Long parentId){
        try {
            PageInfo<TbItemCat> pageInfo = itemCatService.getListItemCat(pageNumber,pageSize,parentId);
            PageUtil<TbItemCat> pageUtil = new PageUtil<>();
            pageUtil.setPageNumber(pageNumber);
            pageUtil.setPageSize(pageSize);
            pageUtil.setList(pageInfo.getList());
            pageUtil.setTotal(pageInfo.getTotal());
            return Result.jsonResponse(ResultEnum.OK).setData(pageUtil);
        }catch (Exception e){
            logger.error("系统报错",e);
            return Result.jsonResponse(ResultEnum.INTERNAL_SERVER_ERROR);
        }
    }

    @RequestMapping("getItemName")
    @ResponseBody
    private Result getItemName(Long id){
        if (null == id || "".equals(id)){
            return Result.jsonResponse(ResultEnum.BAD_REQUEST);
        }
        try {
            TbItemCat tbItemCat = itemCatService.getItemById(id);
            return Result.jsonResponse(ResultEnum.OK).setData(tbItemCat);
        }catch (Exception e){
            logger.error("系统报错",e);
            return Result.jsonResponse(ResultEnum.INTERNAL_SERVER_ERROR);
        }
    }

    @RequestMapping("insertItemCat")
    @ResponseBody
    public Result insertItemCat(TbItemCat tbItemCat){
        if (null == tbItemCat.getName() || "".equals(tbItemCat.getName()) ||
                null == tbItemCat.getParentId() || "".equals(tbItemCat.getParentId()) ||
                null == tbItemCat.getTypeId() || "".equals(tbItemCat.getTypeId())){
            return Result.jsonResponse(ResultEnum.BAD_REQUEST);
        }
        try {
            int i = itemCatService.insertItemCat(tbItemCat);
            return Result.jsonResponse(ResultEnum.OK).setData(tbItemCat);
        }catch (Exception e){
            logger.error("系统报错",e);
            return Result.jsonResponse(ResultEnum.INTERNAL_SERVER_ERROR);
        }
    }

    @RequestMapping("toUpdateItemCat")
    @ResponseBody
    public Result toUpdateItemCat(Long id){
        if (null == id || "".equals(id)){
            return Result.jsonResponse(ResultEnum.BAD_REQUEST);
        }
        try {
            TbItemCat tbItemCat = itemCatService.getTbItemCatById(id);
            return Result.jsonResponse(ResultEnum.OK).setData(tbItemCat);
        }catch (Exception e){
            logger.error("系统报错",e);
            return Result.jsonResponse(ResultEnum.INTERNAL_SERVER_ERROR);
        }
    }

    @RequestMapping("updateItemCat")
    @ResponseBody
    public Result updateItemCat(TbItemCat tbItemCat){
        if (null == tbItemCat.getName() || "".equals(tbItemCat.getName()) ||
                null == tbItemCat.getParentId() || "".equals(tbItemCat.getParentId()) ||
                null == tbItemCat.getTypeId() || "".equals(tbItemCat.getTypeId())){
            return Result.jsonResponse(ResultEnum.BAD_REQUEST);
        }
        try {
            int i = itemCatService.updateTbItemCat(tbItemCat);
            return Result.jsonResponse(ResultEnum.OK);
        }catch (Exception e){
            logger.error("系统报错",e);
            return Result.jsonResponse(ResultEnum.INTERNAL_SERVER_ERROR);
        }
    }

    @RequestMapping("deleteBatch")
    @ResponseBody
    public Result deleteBatch(@RequestParam("ids[]") Long[] ids){
        if (null == ids || "".equals(ids)){
            return Result.jsonResponse(ResultEnum.BAD_REQUEST);
        }
        List<TbItemCat> tbItemCats = itemCatService.getItemByIds(ids);
        if (tbItemCats != null){
            return Result.jsonResponse(ResultEnum.PRECONDITION_FAILED);
        }
        try {
            int i = itemCatService.deleteItemCat(ids);
            return Result.jsonResponse(ResultEnum.OK);
        }catch (Exception e){
            logger.error("系统报错",e);
            return Result.jsonResponse(ResultEnum.INTERNAL_SERVER_ERROR);
        }
    }
}
